package com.paperobot.action;


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.paperobot.common.util.PageUtil;
import com.paperobot.common.util.VOHelper;
import com.paperobot.model.Paper;
import com.paperobot.model.PaperType;
import com.paperobot.model.vo.PaperVO;
import com.paperobot.service.PaperobotService;

public class PaperAction extends BaseAction{

	private static final long serialVersionUID = 1516834338392982332L;
	private PaperobotService paperobotService;;
	private Paper paper;
	private PaperVO paperVO;
	private List<Paper> paperList = new ArrayList<Paper>();
	private List<PaperVO> paperVOList = new ArrayList<PaperVO>();
	private PageUtil pageUtil = new PageUtil();
	private List<PaperType> paperTypeList;
	private List<PaperType> paperSubTypeList;
	private Map<Integer, String> paperTypeMap = new HashMap<Integer, String>();

	
	@SuppressWarnings("unchecked")
	public String listPaper(){
		try {
			String pagetype = request.getParameter("pagetype");
			if(pagetype == null){
				pagetype = "first";
			}
			
			String currentPage = request.getParameter("currentPage");
			if(currentPage != null){
				pageUtil.setCurrentPage(Integer.parseInt(currentPage));
			}
			List<Paper> allPaperList = paperobotService.getPaperList();
			paperList = pageUtil.getCurrentPageRows(pagetype, allPaperList.size(), allPaperList);
			paperTypeList = paperobotService.getPaperTypeList();
			
			paperVOList = VOHelper.paperToVO(paperList, paperTypeList);
			return SUCCESS;
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public void getSubTypeByTypeId(){
		try {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			StringBuffer allSubType = new StringBuffer();
			response.setContentType("text/xml");
			String type = request.getParameter("type");

			paperSubTypeList = paperobotService.getPaperTypeList(Integer.parseInt(type));
			
			allSubType.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
			allSubType.append("<allSubType>");
			for(int i=0; i<paperSubTypeList.size(); i++){
				allSubType.append("<subType>");
				allSubType.append("<subTypeId>" + paperSubTypeList.get(i).getSubType() + "</subTypeId>");
				allSubType.append("<subTypeName>"+ paperSubTypeList.get(i).getSubTypeName() + "</subTypeName>");
				allSubType.append("</subType>");
			}
			allSubType.append("</allSubType>");
			out.print(allSubType.toString());
				
			
//			return SUCCESS;
			
		} catch (Exception e) {
			logger.info(e.getMessage());
//			return ERROR;
		}
	}
	
	public String updatePaper(){
		try {
			String paperId = request.getParameter("paperId");
			paper = paperobotService.getPaper(Long.parseLong(paperId));
			getType();
			
			return "update";
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String deletePaper(){
		try {
			String paperId = request.getParameter("paperId");
			int rs = paperobotService.deletePaper(Long.parseLong(paperId));
			if(rs > 0){
				return "delete";
			}
			return SUCCESS;
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	public String getType(){
		
		try {
			
			List<PaperType> allPaperTypeList = paperobotService.getPaperTypeList();
			
			for(PaperType paperType: allPaperTypeList){
				paperTypeMap.put(paperType.getType(), paperType.getTypeName());
			}
			
			return SUCCESS;
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	public String addPaper(){

		 try {
			 String type = request.getParameter("type");
			 String subType = request.getParameter("subType");
			 String title = request.getParameter("title");
			 String abstracts = request.getParameter("abstracts");
			 String keywords = request.getParameter("keywords");
			 String content = request.getParameter("content");
			 String author = request.getParameter("author");
			 String source = request.getParameter("source");
			 String status = request.getParameter("status");
			 
			 Paper paper = new Paper();
			 paper.setType(Integer.parseInt(type));
			 paper.setSubType(Integer.parseInt(subType));
			 paper.setTitle(title);
			 paper.setAbstracts(abstracts);
			 paper.setKeywords(keywords);
			 paper.setContent(content);
			 paper.setAuthor(author);
			 paper.setSource(source);
			 paper.setCreateTime(new Date());
			 paper.setStatus(Integer.parseInt(status));
			 
			 if(paperobotService.addPaper(paper) > 0){
			 	logger.info("添加论文成功。");
			 	request.setAttribute("result", "添加论文成功！");
			 	return SUCCESS;
			 }else{
				logger.info("添加论文失败。");
			 	request.setAttribute("result", "添加论文失败！");
			 	return ERROR;
			 }
			 
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	
	}

	public String savePaper(){
		try {
			 String paperId = request.getParameter("paperId");
			 String type = request.getParameter("type");
			 String subType = request.getParameter("subType");
			 String title = request.getParameter("title");
			 String abstracts = request.getParameter("abstracts");
			 String keywords = request.getParameter("keywords");
			 String content = request.getParameter("content");
			 String author = request.getParameter("author");
			 String source = request.getParameter("source");
			 String status = request.getParameter("status");
			 
			 Paper paper = new Paper();
			 paper.setPaperId(Long.parseLong(paperId));
			 paper.setType(Integer.parseInt(type));
			 paper.setSubType(Integer.parseInt(subType));
			 paper.setTitle(title);
			 paper.setAbstracts(abstracts);
			 paper.setKeywords(keywords);
			 paper.setContent(content);
			 paper.setAuthor(author);
			 paper.setSource(source);
			 paper.setCreateTime(new Date());
			 paper.setStatus(Integer.parseInt(status));
			 
			 if(paperobotService.updatePaper(paper) > 0){
			 	logger.info("更新论文成功。");
			 	request.setAttribute("result", "更新论文成功！");
			 	return SUCCESS;
			 }else{
				logger.info("更新论文失败。");
			 	request.setAttribute("result", "更新论文失败！");
			 	return ERROR;
			 }
			 
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	@Resource
	public void setPaperobotService(PaperobotService paperobotService) {
		this.paperobotService = paperobotService;
	}
	public PaperVO getPaperVO() {
		return paperVO;
	}
	public void setPaperVO(PaperVO paperVO) {
		this.paperVO = paperVO;
	}
	public List<PaperVO> getPaperVOList() {
		return paperVOList;
	}
	public void setPaperVOList(List<PaperVO> paperVOList) {
		this.paperVOList = paperVOList;
	}
	public Paper getPaper() {
		return paper;
	}
	public void setPaper(Paper paper) {
		this.paper = paper;
	}
	public List<Paper> getPaperList() {
		return paperList;
	}
	public void setPaperList(List<Paper> paperList) {
		this.paperList = paperList;
	}
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	public Map<Integer, String> getPaperTypeMap() {
		return paperTypeMap;
	}
	public void setPaperTypeMap(Map<Integer, String> paperTypeMap) {
		this.paperTypeMap = paperTypeMap;
	}
}
